<template>
	<view class="wosearch">
		<SearchBar :keyword="searchWord" @input="onInput" @search="onSearch" />
		<!--<view class="wosearch-filter">
      <label class="wosearch-checkbox">
        <checkbox style="transform:scale(0.7)" value="cb" checked="true" /><text>搜索服务器</text>
      </label>
      <view class="wosearch-count">搜索到相关工单0个</view>
    </view> -->
		
	   <view v-if="!workOrderList || workOrderList.length ==0"  class="wosearch-count">没有搜索到相关工单</view>
		
		<view class="wosearch-list">
			<block v-for="(listItem, index) in workOrderList" :key="index">
				<WoItem v-for="(orderitem, oindex) in listItem" :key="oindex" :workorder="orderitem" :typealias="firstChar(orderitem.WoType)"
				 @onOpenMap="openAddressMap" />
			</block>
		</view>
		<view class="ag-spacer30"></view>
	</view>
</template>
<script>
	import SearchBar from '../../components/SearchBar';
	import WoItem from '../../components/WoItem';
	export default {
		components: {
			SearchBar,
			WoItem
		},
		data() {
			return {
				searchWord: '',
				workOrderList: [],
				ramwOrderList: []
			}
		},
		onLoad(e) {
			this.searchWord = e.searchWord || '';
			if (this.searchWord) {
				this.onSearch(this.searchWord);
			}
		},
		onReachBottom(e) {
			uni.showLoading();
			if (this.ramwOrderList.length > 0) {
				let subOrderArray = this.ramwOrderList.splice(0, 10);
				this.workOrderList.push(subOrderArray);
			}
			uni.hideLoading();
		},
		methods: {
			onInput(e) {
				this.searchWord = e;
				console.log(this.searchWord)
			},
			onSearch(e) {
                uni.showLoading({
					mask:true
				});
				this.searchWord = e;
				console.log(this.searchWord);
				this.ramwOrderList = [];
				this.workOrderList = [];
				this.WOListSearchAsync().then((succ) => {
						let {
							RetBool,
							RetObject
						} = succ;
						if (RetBool) {
							if (RetObject) {
								this.ramwOrderList = this.uniqueArray(JSON.parse(RetObject));
								let subOrderArray = this.ramwOrderList.splice(0, 10);
								console.log(subOrderArray);
								this.workOrderList.push(subOrderArray);
							} else {
								uni.showToast({
									title: "没有符合条件的工单",
									icon: "none"
								});
							}
							uni.hideLoading();
						}
					},
					(err) => {
						console.log(err);
						uni.hideLoading();
					})
			},
			openAddressMap(e) {
				let token = this.UserInfo.RetId;
				//let locationMap = await 

				this.getAddressMapAsync(e.adddress).then((locationMap) => {
					uni.openLocation({
						latitude: locationMap.result.location.lat,
						longitude: locationMap.result.location.lng,
						success: function() {
							console.log('success');
						}
					});
				}, (err) => {
					console.log(err);
					uni.showToast({
						title: "地址转换出错",
						icon: "none"
					})
				})
			},

			//地址转经纬度
			async getAddressMapAsync(address) {
				let result = await this.mapApiService.geoCommMapAsync(this.qqMapServiceUrl,
					"/ws/geocoder/v1",
					"address",
					address);
				return result;
			},

			async WOListSearchAsync() {
				let userPhone = this.UserInfo.mobile;
				let token = this.UserInfo.RetId;
				let searchWord = this.searchWord;
				let result = await this.workorderlistervice
					.WOListSearchAsync(userPhone, searchWord, token);
				return result;
			},
			firstChar(str) {
				if (str)
					return str.charAt(0);
				else
					return ""
			},
			//去掉重复数据
			uniqueArray(arrobj) {
				let res = [];
				for (let i in arrobj) {
					res.findIndex((item, index, arr) => {
						return item.WoId === arrobj[i].WoId
					}) !== -1 ? '' : res.push(arrobj[i]);
				}
				return res;
			}
		}
	}
</script>
<style lang="scss">
	@import "./index.scss";
</style>
